package mi.payment.transaction.sale.code;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import mi.payment.R;
import mi.payment.admin.pay88.IPay88Wallet;
import mi.payment.admin.pay88.WebServiceApi;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.AccountType;
import mi.payment.constants.Params;
import mi.payment.db.PayDetailModel;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.BaseScanActivity;
import mi.payment.ui.base.PageRoute;
import mi.payment.ui.qr.code.EnterPaymentCodeActivity;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.SettingHomePageUtils;
import mi.payment.utils.UIUtils;
import sunmi.payment.L3.L3Params;

/**
 * 扫码支付界面
 */
public class ScanActivity extends BaseScanActivity {

    private String mAmount;
    private IPay88Wallet mIPay88Wallet;

    private TradeData mTradeData;
    private PayDetail mPayDetail;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        initTradeData();
    }

    private void initTradeData() {
        Intent intent = getIntent();
        mAmount = intent.getStringExtra("amount");
        mIPay88Wallet = (IPay88Wallet) intent.getSerializableExtra("wallet");
        long amountLong = MoneyUtils.stringMoney2LongCent(mAmount);

        mTradeData = Params.newTradeData();
        mPayDetail = mTradeData.getPayDetail();
        mPayDetail.qrCodeScanModel = 1;
        mPayDetail.amount = amountLong;
        mPayDetail.currency = mIPay88Wallet.currency;
        mPayDetail.accountType = AccountType.OA.getValue();
        // L3自定义订单号赋值
        L3Params l3Params = Params.sL3Params;
        if (l3Params != null) {
            mPayDetail.customOrderNo = l3Params.transId;
        }

        mPayDetail.transType = "MP";
        try {
            mPayDetail.transPlatform = Integer.parseInt(mIPay88Wallet.merchantPaymentID);
        } catch (Exception e) {
            mPayDetail.transPlatform = 1;
        }

        SettingHomePageUtils.getInstance().addScan();
    }

    @Override
    protected String getScanHintText() {
        return getString(R.string.money_symbol) + mAmount;
    }

    @Override
    protected String getClickText() {
        return getString(R.string.can_not_scan_click);
    }

    @Override
    protected String getBarTitle() {
        if (mPayDetail != null) {
            return UIUtils.getPaymentType(mPayDetail);
        } else {
            return "";
        }
    }

    @Override
    protected View.OnClickListener getOnClickListener() {
        return v -> {
            Intent inputCodeIntent = new Intent(ScanActivity.this, EnterPaymentCodeActivity.class);
            // inputCodeIntent.putExtra("platform", mPlatform);
            inputCodeIntent.putExtra("amount", mAmount);
            Bundle bundle = new Bundle();
            bundle.putSerializable(EnterPaymentCodeActivity.QR_CODE_PAY_TRADE_DATA, mTradeData);
            inputCodeIntent.putExtras(bundle);
            startActivity(inputCodeIntent);
        };
    }

    @Override
    protected void onScanSuccess(String codeValue) {
        mPayDetail.setPayQRCode(codeValue);
        startTransaction();
    }

    private void startTransaction() {
        // 消费
        mTradeData.getPayDetail().setTransactionType(1);
        showLoadingDialog(R.string.load_connecting);
        executeTask(
                () -> {
                    try {
                        TradeData tradeData = WebServiceApi.getInstance().saleTransaction(mTradeData, true);
                        PayDetail resPay = tradeData.getPayDetail();
                        PayDetailModel.getInstance().insert(resPay);
                        PageRoute.startAction(mActivity, PageRoute.TYPE_QR_CODE_CONSUME, resPay);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        dismissLoadingDialog();
                    }
                }
        );
    }

    public static void startAction(Context context, String amount, int platform) {
        Intent intent = new Intent(context, ScanActivity.class);
        intent.putExtra("amount", amount);
        intent.putExtra("platform", platform);
        context.startActivity(intent);
    }

    public static void startAction(Context context, String amount, IPay88Wallet wallet) {
        Intent intent = new Intent(context, ScanActivity.class);
        intent.putExtra("amount", amount);
        intent.putExtra("wallet", wallet);
        context.startActivity(intent);
    }


}
